package sk.fiit.vppj.eventmanager.gui.judge;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import sk.fiit.vppj.eventmanager.core.ErrorHandler;
import sk.fiit.vppj.eventmanager.core.UserManager;
import sk.fiit.vppj.eventmanager.core.Utils;
import sk.fiit.vppj.eventmanager.data.Project;
import sk.fiit.vppj.eventmanager.data.ProjectSingleEval;
import sk.fiit.vppj.eventmanager.gui.administrator.ProjectTable;
import sk.fiit.vppj.eventmanager.gui.administrator.ProjectsTable;

final class JudgeGeneralPanel extends JPanel implements ProjectTable {
	private JPanel tablePanel = new ProjectsTable( this );
	private JPanel projectPanel = null;
	private Project project = null;
	private Evaluation evaluation = null;

	public JudgeGeneralPanel() {
		setLayout( new GridLayout( 1, 2 ) );
		add( tablePanel );
		this.projectPanel = new JPanel();
		this.projectPanel.setLayout( new BoxLayout( projectPanel, BoxLayout.Y_AXIS ) );
		this.projectPanel.setAlignmentX( LEFT_ALIGNMENT );
		add( this.projectPanel );

	}

	public void showProjectData(Project selectedProject) {
		this.project = selectedProject;

		this.projectPanel.removeAll();
		refreshPersonalPanel();

		this.projectPanel.updateUI();
	}

	private void refreshPersonalPanel() {
		StringBuilder body = new StringBuilder( 4000 );
		body.append( "<html>" );
		body.append( "<h2>" );
		body.append( this.project.getEngName() );
		body.append( "</h2><br><b>Category:</b> " );
		body.append( this.project.getCategoryName() );
		body.append( "<br><b>Team project:</b> " );
		if( this.project.isTeamProject() ) {
			body.append( "Yes" );
		}
		else {
			body.append( "No" );
		}
		body.append( "<br><b>English abstract:</b> " );
		body.append( this.project.getEngAbstract() );
		body.append( "</html>" );
		JLabel label = new JLabel( body.toString() );
		label.setAlignmentX( LEFT_ALIGNMENT );
		label.setMinimumSize( new Dimension( this.getWidth(), this.getHeight() ) );
		this.projectPanel.add( label );
		showEvaluationTable();
	}

	private void showEvaluationTable() {
		evaluation = new Evaluation();
		final List<ProjectSingleEval> evals = evaluation.getSingleEvaluations( this.project.getId(), UserManager.getInstance().getUser()
				.getId() );
		final List<JTextField> evalFields = new ArrayList<JTextField>( evals.size() );
		JPanel panel = getEvaluationZone( evalFields, evals );

		panel.add( getSubmitButton( evalFields, evals ) );
		this.projectPanel.add( panel );

	}

	private JPanel getEvaluationZone(List<JTextField> evalFields, List<ProjectSingleEval> evals) {
		JLabel label = null;
		StringBuilder labelText = new StringBuilder( 200 );
		JPanel panel = new JPanel( new FlowLayout( FlowLayout.CENTER ) );

		for ( int i = 0; i < evals.size(); i++ ) {
			labelText.append( evals.get( i ).getCriteriaName() );
			labelText.append( "( " );
			labelText.append( evals.get( i ).getMaxPoints() );
			labelText.append( "pts )" );
			label = new JLabel( labelText.toString() );
			label.setToolTipText( evals.get( i ).getCriteriaDescription() );
			panel.add( label );
			evalFields.add( new JTextField( String.valueOf( evals.get( i ).getPoints() ), 3 ) );
			panel.add( evalFields.get( i ) );
			labelText.delete( 0, labelText.length() );
		}
		return panel;
	}

	private JButton getSubmitButton(final List<JTextField> evalFields, final List<ProjectSingleEval> evals) {
		JButton submitButton = new JButton( "Submit" );
		submitButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				for ( int i = 0; i < evals.size(); i++ ) {
					if( !Utils.isNumber( evalFields.get( i ).getText() )
							|| evals.get( i ).getMaxPoints() < Integer.parseInt( evalFields.get( i ).getText() ) ) {
						ErrorHandler.getInstance().showErrorMessage(
								"You've inserted invalid values or you gave more points to project than max. limit" );
						continue;
					}
					evaluation.saveEvaluation( evals.get( i ).getEvalId(), UserManager.getInstance().getUser().getEvent_id(),
							project.getId(), UserManager.getInstance().getUser().getId(), evals.get( i ).getCriteriaId(),
							Integer.parseInt( evalFields.get( i ).getText() ) );
					showProjectData( project );
				}
			}
		} );
		return submitButton;
	}
}
